package org.devf.fork.imsimulator.view.extend;

import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import org.devf.fork.imsimulator.socket.dto.MsgStatus;
import org.devf.fork.imsimulator.view.layout.ChatItem;

/**
 * ChatMessageListCel related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/25 11:44
 */
public class ChatMessageListCel extends ListCell<ChatItem> {


    public ChatMessageListCel() {

        itemProperty().addListener((observable, oldValue, newValue) -> {
            if (null != newValue) {
                newValue.getMessageItem()
                        .msgStatusProperty()
                        .addListener((statusObservable, oldStatus, newStatus) -> {
                            if (null == newStatus) {
                                return;
                            }
                            if (null != getGraphic()) {
                                Label status = (Label) getGraphic().lookup("#message-status-label");
                                System.out.println(oldStatus + "消息状态变化！！" + newStatus);
                                status.setText(MsgStatus.getByCode((Integer) newStatus).getDesc());
                            }
                        });

            }

        });

    }

    @Override
    protected void updateItem(ChatItem item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null) {
            // 如果列表项为空或没有ChatItem对象，清空列表项
            setText(null);
            setGraphic(null);
            return;
        }
        setGraphic(item);

    }
}
